<?php

namespace app\admin\model\zhongtai;

use think\Model;

class ShareMoney extends Model
{
    // 表名
    protected $table = 'share_money';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';


    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }
    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $user_data = (new User())->getUserStatus($manage_id,$v['user_id']);
                if(!$user_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "share_money_id" => $v['id'],
                    "money" => $v['money'],
                    "order_id" => $v['order_id'],
                    "user_id" => $v['user_id'],
                    "goods_id" => $v['goods_id'],
                    "role" => $v['role'],
                    "day_time" => $v['day_time'],
                    "type" => $v['type'],
                    "mutual_money" => $v['mutual_money'],
                    "share_user_id" => $v['share_user_id'],
                    "user_flags" => $v['user_flags'],
                    "is_dsh" => $v['is_dsh'],
                    "is_tb" => $v['is_tb'],
                    "shop_id" => $v['shop_id'],
                    "is_grant" => $v['is_grant'],
                    "addtime" => $v['addtime'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'share_money_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'share_money_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "share_monry表更新完成";
    }
}
